
package swa.e_store.userInterface;

import java.awt.ComponentOrientation;
import java.text.ParseException;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import swa.e_store.DTO.ArticleDTO;
import swa.e_store.DTO.OfferDTO;
import swa.e_store.DTO.ShopReportDTO;
import swa.e_store.DTO.StockDTO;
import swa.e_store.ENUM.EmployeeState;
import swa.e_store.businessLogic.BL_Interface;
import swa.e_store.businessLogic.BL_RemoteFacade;
import swa.e_store.businessLogic.BL_Report;


/**
 * LogicUI class contains all logic needed to build up main graphical user
 * interface by the GraphicalUI class.
 *
 * @author Stefan Schibig (stefan.schibig@stud.hslu.ch
 */
public class LogicUI {

    private GraphicalUI gui;
    private GUIListener guiListener;
    
    private BL_Interface bLogic;
    
    public LogicUI(GraphicalUI gui){
        this.gui = gui;
        guiListener = new GUIListener(this);
        
        // Choose Businesslogic.
        this.bLogic = new BL_RemoteFacade();
    }

    /*
     * This method creates the menuBar and returns the
     * menuBar created.
     * 
     * @return  JMenuBar   the JMenuBar created.
     */
    protected JMenuBar createMenubar(EmployeeState eState){
        //Create MenuBar
        JMenuBar menuBar = new JMenuBar();

        //Create Menus
        JMenu jMenuFile = new JMenu("File");
        JMenu jMenuEdit = new JMenu("Edit");
        JMenu jMenuInfo = new JMenu("Info");

        //Put Menus onto MenuBar
        menuBar.add(jMenuFile);
        menuBar.add(jMenuEdit);
        menuBar.add(jMenuInfo);

        //Put menuItems onto menus depending the empState.
        //DateiMenuItems
        for(JMenuItem item : getMenuItemsDatei(eState)){
            jMenuFile.add(item);
        }
        
        return menuBar;
    }

    /*
     *
     * @param  
     * @return   
     */
    private ArrayList<JMenuItem> getMenuItemsDatei(EmployeeState eState) {
        ArrayList<JMenuItem> itemListFile = new ArrayList<JMenuItem>();

        switch(eState){
            case BRANCHMNGR:{
                itemListFile.add(getShowArticleTableItem());
                itemListFile.add(getShowOffersItem());
                itemListFile.add(getCISOrderItem());
                itemListFile.add(getArticlesByOfferItem());
                itemListFile.add(getPurchaseItem());
                itemListFile.add(getShowStockTableItem());
                itemListFile.add(getShopReportItem());
                itemListFile.add(getQuitItem());

                return itemListFile;
            }
            case SYSADMIN:{
                itemListFile.add(getShowArticleTableItem());
                itemListFile.add(getShowOffersItem());
                itemListFile.add(getCISOrderItem());
                itemListFile.add(getArticlesByOfferItem());
                itemListFile.add(getPurchaseItem());
                itemListFile.add(getShowStockTableItem());
                itemListFile.add(getQuitItem());
                return itemListFile;
            }
            case ASSISTANT:{
                itemListFile.add(getShowArticleTableItem());
                itemListFile.add(getArticlesByOfferItem());
                itemListFile.add(getShowOffersItem());
                itemListFile.add(getPurchaseItem());
                itemListFile.add(getQuitItem());
                return itemListFile;
            }

        default:{
                itemListFile.add(getQuitItem());
                return itemListFile;
            }
        }
    }



    private JMenuItem getArticlesByOfferItem(){
        JMenuItem articlesByOfferItem = new JMenuItem("Show Articles by Offer");
        articlesByOfferItem.addActionListener(guiListener);

        return articlesByOfferItem;
    }


    private JMenuItem getShowStockTableItem(){

        JMenuItem showStockTableItem = new JMenuItem("Show Stock");
        showStockTableItem.addActionListener(guiListener);

        return showStockTableItem;
    }


    private JMenuItem getShopReportItem(){
        JMenuItem shopReportItem = new JMenuItem("Show Shop Report");
        shopReportItem.addActionListener(guiListener);

        return shopReportItem;
    }


    private JMenuItem getCISOrderItem(){
        JMenuItem cISOrderItem = new JMenuItem("Order from Central System");
        cISOrderItem.addActionListener(guiListener);

        return cISOrderItem;
    }



    private JMenuItem getPurchaseItem(){
        JMenuItem purchaseItem = new JMenuItem("Take Purchase Order");
        purchaseItem.addActionListener(guiListener);

        return purchaseItem;
    }



    private JMenuItem getShowArticleTableItem(){
        JMenuItem showArticleTableItem = new JMenuItem("Show Article Table");
        showArticleTableItem.addActionListener(guiListener);

        return showArticleTableItem;
    }



    private JMenuItem getShowOffersItem(){
        JMenuItem showOffersItem = new JMenuItem("Show Offers");
        showOffersItem.addActionListener(guiListener);

        return showOffersItem;
    }



    private JMenuItem getQuitItem(){
        JMenuItem quitItem = new JMenuItem("Quit");
        quitItem.addActionListener(guiListener);

        return quitItem;
    }

  
    protected void actionShowArticleTable() {
        
        ArticleDTO artDTO = bLogic.getArticleTable();
        
        JTable table = new JTable(artDTO.getData(),artDTO.getColumnNames());
        table.getModel().addTableModelListener(new TableListener());
        
        gui.setContentPane(table);
        gui.pack();
        
    }


    protected void actionCISOrder() {
        
        ReOrderPanel reOrderPanel = new ReOrderPanel();
          gui.setContentPane(reOrderPanel);
          gui.pack();
    }

    public void actionShowArticleByOffer() {
        
        ShowArticlesByOfferPanel panel = new ShowArticlesByOfferPanel(gui);
        gui.setContentPane(panel);
        gui.pack();
    }

    protected void actionPurchaseOrder() {
        PurchasePanel purchasePanel = new PurchasePanel();
          gui.setContentPane(purchasePanel);
          gui.pack();
    }

    protected void actionShowOffers() {
        
        OfferDTO offerDTO = bLogic.getOfferTable();
        
        JTable table = new JTable(offerDTO.getData(),offerDTO.getColumnNames());
        
        gui.setContentPane(table);
        gui.pack();
    }

    protected void actionShowStock() {
        StockDTO stockDTO = bLogic.getStock();
        
        JTable table = new JTable(stockDTO.getData(),stockDTO.getColumnNames());
        
        gui.setContentPane(table);
        gui.pack();
    }
    
    protected void actionShopReport() {
        ShopReportDTO shopReportDTO = bLogic.getShopReport();
        
        JTable table = new JTable(shopReportDTO.getData(), shopReportDTO.getColumnNames());
        
        gui.setContentPane(table);
        gui.pack();
    }

    protected void actionQuit() {
        gui.setVisible(false);
        gui.dispose();
    }
}


